<template>
  <div class="manual-trigger">
    <el-card class="trigger-card">
      <template #header>
        <div class="card-header">
          <h3>手动触发任务</h3>
        </div>
      </template>
      
      <el-form :model="form" label-width="120px">
        <el-form-item label="设备名称">
          <el-select v-model="form.DEVICE_NAME" placeholder="请选择设备">
            <el-option
              v-for="device in deviceList"
              :key="device"
              :label="device"
              :value="device">
            </el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="任务参数">
          <el-input
            v-model="form.params"
            type="textarea"
            :rows="10"
            placeholder="请输入JSON格式的任务参数"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">触发任务</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const deviceList = ref(['Device1', 'Device2', 'Device3']) // 这里需要根据实际设备列表修改

const form = ref({
  DEVICE_NAME: '',
  params: ''
})

const handleSubmit = async () => {
  try {
    if (!form.value.DEVICE_NAME) {
      ElMessage.error('请选择设备')
      return
    }
    
    let params = {}
    try {
      params = JSON.parse(form.value.params)
    } catch (e) {
      ElMessage.error('JSON格式不正确')
      return
    }
    
    const response = await axios.post('/manual_trigger', {
      DEVICE_NAME: form.value.DEVICE_NAME,
      ...params
    })
    
    if (response.data.status === 'success') {
      ElMessage.success('任务触发成功')
    } else {
      ElMessage.error(response.data.message || '任务触发失败')
    }
  } catch (error) {
    ElMessage.error('请求失败：' + error.message)
  }
}
</script>

<style scoped>
.manual-trigger {
  max-width: 800px;
  margin: 0 auto;
}

.trigger-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>